package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.ClientAddress;
import com.ruoyi.recycle.domain.ClientUserManagement;
import com.ruoyi.recycle.domain.HousekeepingOrder;
import com.ruoyi.recycle.dto.ClientUserManagementDTO;
import com.ruoyi.recycle.service.IClientUserManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 普通用户管理Controller
 * 
 * @author Wubk
 * @date 2022-05-21
 */
@RestController
@RequestMapping("/client")
@Api(tags = "普通用户管理")
public class ClientUserManagementController extends BaseController
{
    @Autowired
    private IClientUserManagementService clientUserManagementService;

    /**
     * 查询普通用户管理列表
     */
    @RequiresPermissions("user:client:list")
    @GetMapping("/list")
    public TableDataInfo list(ClientUserManagement clientUserManagement)
    {
        startPage();
        List<ClientUserManagement> list = clientUserManagementService.selectClientUserManagementList(clientUserManagement);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/clientPageQuery")
    @ApiOperation("分页条件查询")
    public IPage<ClientUserManagement> clientPageQuery(@RequestBody ClientUserManagementDTO queryDTO) throws Exception{
        final ClientUserManagement client = new ClientUserManagement();
        BeanUtils.copyProperties(queryDTO, client);
        final LambdaQueryWrapper<ClientUserManagement> wrapper = Wrappers.lambdaQuery(client);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(ClientUserManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(ClientUserManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(ClientUserManagement::getCreatedTime);
        return clientUserManagementService.page(new Page<ClientUserManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 添加积分
     */
    @GetMapping("/saveIntegral")
    @ApiOperation(value = "添加积分")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long"),
            @ApiImplicitParam(name = "integral", value = "积分", dataType = "Long")})
    public AjaxResult saveIntegral(@RequestParam("id") Long id,@RequestParam("integral") Long integral) {
        return clientUserManagementService.saveIntegral(id,integral);
    }

    /**
     * 导出普通用户管理列表
     */
    @RequiresPermissions("user:client:export")
    @Log(title = "普通用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出普通用户管理列表")
    public void export(HttpServletResponse response, ClientUserManagement clientUserManagement)
    {
        List<ClientUserManagement> list = clientUserManagementService.selectClientUserManagementList(clientUserManagement);
        ExcelUtil<ClientUserManagement> util = new ExcelUtil<ClientUserManagement>(ClientUserManagement.class);
        util.exportExcel(response, list, "普通用户管理数据");
    }

    /**
     * 获取普通用户管理详细信息
     */
    @RequiresPermissions("user:client:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取普通用户管理详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(clientUserManagementService.selectClientUserManagementById(id));
    }

    /**
     * 新增普通用户管理
     */
    @RequiresPermissions("user:client:add")
    @Log(title = "普通用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增普通用户管理")
    public AjaxResult add(@RequestBody ClientUserManagement clientUserManagement)
    {
        return toAjax(clientUserManagementService.insertClientUserManagement(clientUserManagement));
    }

    /**
     * 修改普通用户管理
     */
    @RequiresPermissions("user:client:edit")
    @Log(title = "普通用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改普通用户管理")
    public AjaxResult edit(@RequestBody ClientUserManagement clientUserManagement)
    {
        return toAjax(clientUserManagementService.updateClientUserManagement(clientUserManagement));
    }

    /**
     * 删除普通用户管理
     */
    @RequiresPermissions("user:client:remove")
    @Log(title = "普通用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除普通用户管理")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(clientUserManagementService.deleteClientUserManagementByIds(ids));
    }

    /**
     * 新增用户地址
     */
    @Log(title = "新增用户地址", businessType = BusinessType.UPDATE)
    @PostMapping("/addAddress")
    @ApiOperation(value = "新增用户地址")
    public AjaxResult addAddress(@RequestBody ClientAddress clientAddress)
    {
        return clientUserManagementService.addAddress(clientAddress);
    }

    /**
     * 修改用户地址
     */
    @Log(title = "修改用户地址", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改用户地址")
    public AjaxResult updateAddress(@RequestBody ClientAddress clientAddress)
    {
        return clientUserManagementService.updateAddress(clientAddress);
    }

    /**
     * 删除用户地址
     */
    @Log(title = "删除用户地址", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteAddress/{ids}")
    @ApiOperation(value = "删除用户地址")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult deleteAddress(@PathVariable Long[] ids)
    {
        return clientUserManagementService.deleteAddress(ids);
    }

    /**
     * 获取用户地址列表
     */
    @Log(title = "获取用户地址列表", businessType = BusinessType.UPDATE)
    @PostMapping("/getClientAddress")
    @ApiOperation(value = "获取用户地址列表")
    public AjaxResult getClientAddress(@RequestParam(value = "clientId",required = true) Integer clientId){
        return clientUserManagementService.getClientAddress(clientId);
    }



}
